package org.htmlparser.parserapplications.filterbuilder.wrappers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.RegexFilter;
import org.htmlparser.parserapplications.filterbuilder.Filter;

public class RegexFilterWrapper extends Filter
  implements ActionListener, DocumentListener
{
  public static Object[][] mMap = new Object[3][];
  protected RegexFilter mFilter = new RegexFilter();
  protected JTextArea mPattern = new JTextArea(2, 20);
  protected JComboBox mStrategy;

  static
  {
    mMap[0] = new Object[2];
    mMap[0][0] = "MATCH";
    mMap[0][1] = new Integer(1);
    mMap[1] = new Object[2];
    mMap[1][0] = "LOOKINGAT";
    mMap[1][1] = new Integer(2);
    mMap[2] = new Object[2];
    mMap[2][0] = "FIND";
    mMap[2][1] = new Integer(3);
  }

  public RegexFilterWrapper()
  {
    this.mPattern.setBorder(new BevelBorder(1));
    add(this.mPattern);
    this.mPattern.getDocument().addDocumentListener(this);
    this.mPattern.setText(this.mFilter.getPattern());
    this.mStrategy = new JComboBox();
    this.mStrategy.addItem("MATCH");
    this.mStrategy.addItem("LOOKINGAT");
    this.mStrategy.addItem("FIND");
    add(this.mStrategy);
    this.mStrategy.addActionListener(this);
    this.mStrategy.setSelectedIndex(strategyToIndex(this.mFilter.getStrategy()));
  }

  public boolean accept(Node paramNode)
  {
    return this.mFilter.accept(paramNode);
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    if (paramActionEvent.getSource() == this.mStrategy)
      this.mFilter.setStrategy(indexToStrategy(this.mStrategy.getSelectedIndex()));
  }

  public void changedUpdate(DocumentEvent paramDocumentEvent)
  {
  }

  public String getDescription()
  {
    return "Nodes containing regex";
  }

  public String getIconSpec()
  {
    return "images/RegexFilter.gif";
  }

  public NodeFilter getNodeFilter()
  {
    RegexFilter localRegexFilter = new RegexFilter();
    localRegexFilter.setStrategy(this.mFilter.getStrategy());
    localRegexFilter.setPattern(this.mFilter.getPattern());
    return localRegexFilter;
  }

  public NodeFilter[] getSubNodeFilters()
  {
    return new NodeFilter[0];
  }

  public int indexToStrategy(int paramInt)
  {
    return ((Integer)mMap[paramInt][1]).intValue();
  }

  public void insertUpdate(DocumentEvent paramDocumentEvent)
  {
    Document localDocument = paramDocumentEvent.getDocument();
    try
    {
      this.mFilter.setPattern(localDocument.getText(0, localDocument.getLength()));
      return;
    }
    catch (BadLocationException localBadLocationException)
    {
      localBadLocationException.printStackTrace();
    }
  }

  public void removeUpdate(DocumentEvent paramDocumentEvent)
  {
    Document localDocument = paramDocumentEvent.getDocument();
    try
    {
      this.mFilter.setPattern(localDocument.getText(0, localDocument.getLength()));
      return;
    }
    catch (BadLocationException localBadLocationException)
    {
      localBadLocationException.printStackTrace();
    }
  }

  public void setNodeFilter(NodeFilter paramNodeFilter, Parser paramParser)
  {
    this.mFilter = ((RegexFilter)paramNodeFilter);
    this.mPattern.setText(this.mFilter.getPattern());
    this.mStrategy.setSelectedIndex(strategyToIndex(this.mFilter.getStrategy()));
  }

  public void setSubNodeFilters(NodeFilter[] paramArrayOfNodeFilter)
  {
  }

  public int strategyToIndex(int paramInt)
  {
    for (int i = 0; i < mMap.length; ++i)
      if (paramInt == ((Integer)mMap[i][1]).intValue())
        return i;
    throw new IllegalArgumentException("unknown strategy constant - " + paramInt);
  }

  public String strategyToString(int paramInt)
  {
    for (int i = 0; i < mMap.length; ++i)
      if (paramInt == ((Integer)mMap[i][1]).intValue())
        return ((String)mMap[i][0]);
    throw new IllegalArgumentException("unknown strategy constant - " + paramInt);
  }

  public int stringToStrategy(String paramString)
  {
    for (int i = 0; i < mMap.length; ++i)
      if (paramString.equalsIgnoreCase((String)mMap[i][0]))
        return ((Integer)mMap[i][1]).intValue();
    throw new IllegalArgumentException("unknown strategy constant - " + paramString);
  }

  public String toJavaCode(StringBuffer paramStringBuffer, int[] paramArrayOfInt)
  {
    StringBuffer localStringBuffer = new StringBuffer().append("filter");
    int i = paramArrayOfInt[1];
    paramArrayOfInt[1] = (i + 1);
    String str = i;
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append("RegexFilter ");
    paramStringBuffer.append(str);
    paramStringBuffer.append(" = new RegexFilter ();");
    newline(paramStringBuffer);
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append(str);
    paramStringBuffer.append(".setStrategy (RegexFilter.");
    paramStringBuffer.append(strategyToString(this.mFilter.getStrategy()));
    paramStringBuffer.append(");");
    newline(paramStringBuffer);
    spaces(paramStringBuffer, paramArrayOfInt[0]);
    paramStringBuffer.append(str);
    paramStringBuffer.append(".setPattern (\"");
    paramStringBuffer.append(this.mFilter.getPattern());
    paramStringBuffer.append("\");");
    newline(paramStringBuffer);
    return str;
  }
}